package com.cg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cg.dto.*;
import com.cg.entity.Inheritor;
import com.cg.entity.UserLike;
import com.cg.mapper.InheritorMapper;
import com.cg.result.R;
import com.cg.service.AccountService;
import com.cg.service.IInheritorService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cg.service.ILikeService;
import com.cg.vo.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 加
 * @since 2025-01-17
 */
@Service
public class InheritorServiceImpl extends ServiceImpl<InheritorMapper, Inheritor> implements IInheritorService {

    @Autowired
    private AccountService accountService;
    @Autowired
    private ILikeService likeService;
    @Autowired
    private IInheritorService inheritorService;

    @Override
    public R addInheritor(AddInheritorDTO addInheritorDTO) {
            Inheritor inheritor = new Inheritor();
            BeanUtils.copyProperties(addInheritorDTO, inheritor);
            save(inheritor);
            return R.Success("新增成功");
    }

    @Override
    public R updateInheritor(UpdateInheritorDTO updateInheritorDTO) {
        Inheritor inheritor=getById(updateInheritorDTO.getInheritorId());
        BeanUtils.copyProperties(updateInheritorDTO,inheritor);
        updateById(inheritor);
        return R.Success("修改成功");
    }

    @Override
    public R<List<Inheritor>> searchInheritorByCondition(SearchInheritorDTO searchInheritorDTO) {
        LambdaQueryWrapper<Inheritor> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(searchInheritorDTO.getInheritorName()!=null,Inheritor::getInheritorName,searchInheritorDTO.getInheritorName());


        Page<Inheritor> inheritorPage=new Page<>(searchInheritorDTO.getPageNumber(),searchInheritorDTO.getPageSize());
        inheritorPage=page(inheritorPage,queryWrapper);
        PageVO<Inheritor> inheritorPageVO=new PageVO<>();
        BeanUtils.copyProperties(inheritorPage,inheritorPageVO);
        return R.Success(inheritorPageVO);
    }

    @Override
    public R getInheritorDetail(Long inheritorId) {
        Inheritor inheritor=getById(inheritorId);
        return R.Success(inheritor);
    }

    @Override
    public R addLike(AddLikeDTO addLikeDTO) {
        UserLike like=new UserLike();
        like.setInheritorId(addLikeDTO.getInheritorId());
        like.setUserId(accountService.getCurrentUserId());
        like.setContent(inheritorService.selectContentById(addLikeDTO.getInheritorId()));
        likeService.save(like);
        return R.Success("收藏传人成功");
    }

    @Override
    public String selectContentById(Long inheritorId) {
        Inheritor inheritor=getById(inheritorId);
        return inheritor.getInheritorName();
    }

}
